﻿using Microsoft.EntityFrameworkCore;

namespace 多对多
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using(var db =new MyContext())
            {/*
                Student s1 = new Student() { Name = "张三" };
                Student s2 = new Student() { Name = "李四" };
                Student s3 = new Student() { Name = "王五" };

                Teacher t1 = new Teacher() { Name = "Tom" };
                Teacher t2 = new Teacher() { Name = "Lily" };
                Teacher t3 = new Teacher() { Name = "Lucy" };

                s1.Teachers.Add(t1);
                s1.Teachers.Add(t2);

                s2.Teachers.Add(t2);
                s2.Teachers.Add(t3);

                s3.Teachers.Add(t1);
                s3.Teachers.Add(t2);
                s3.Teachers.Add(t3);

                db.Students.Add(s1);
                db.Students.Add(s2);
                db.Students.Add(s3);
                db.SaveChanges();
                */

                //查询所有老师及每个老师有哪些学生
                var teathers = db.Teachers.Include(t => t.Students);
                foreach(var teacher in teathers)
                {
                    Console.WriteLine(teacher.Name);
                    foreach(var student in teacher.Students)
                    {
                        Console.WriteLine($"\t{student.Name}");
                    }
                }

            }
        }
    }
}
